const Koa = require('koa');
const app = new Koa();
const router = require('@koa/router')()
const bodyParser = require('koa-bodyparser');
const jwt = require('jsonwebtoken')
const cors = require('koa-cors')
const global = require('./utils/mysql')
const moment = require('moment')

// 登录
router.post('/login', async ctx => {
    let { name, pwd } = ctx.request.body

    let res = await global.query(`select * from user where name = '${name}'`)
    if (res.length == 0) {

        ctx.body = { code: -1, msg: '用户名不存在' }
        return;
    }

    let ress = await global.query(`select * from user where pwd = '${pwd}'`)
    if (ress.length == 0) {
        ctx.body = { code: 0, msg: '密码不正确' }
        return;
    }

    let result = await global.query(`select * from user where name = '${name}' and pwd = '${pwd}'`)
    if (result.length > 0) {

        let token = jwt.sign({ id: result[0].id, name: result[0].name }, 'byp', { expiresIn: '1h' })

        ctx.body = { code: 1, msg: '登录成功', token }
    }
})

// 获取数据
router.get('/list', async ctx => {
    let res = await global.query(`select * from list`)
    ctx.body = {
        code: 1,
        msg: "获取成功",
        data: res
    }
})

// 搜索
router.post('/list/search', async ctx => {
    let { title, status, author } = ctx.request.body;


    let sql = ` select * from list where 1 = 1 `

    if (title != "") {
        sql += ` and title like '%${title}%'`
    }
    if (status != "") {
        sql += ` and status  = '${status}'`
    }
    if (author != "") {
        sql += ` and author  = '${author}'`
    }

    let res = await global.query(sql)
    ctx.body = {
        code: 1,
        msg: "获取成功",
        data: res
    }
})

// 新增数据
router.post('/list/add', async ctx => {
    let { title, status, author, type, ing } = ctx.request.body;

    let createTime = moment().format('YYYY MM DD HH:mm:ss')

    let res = await global.query(`insert into list(title,author,status,type,createTime,ing) values('${title}','${author}','${status}','${type}','${createTime}','${ing}')`)
    ctx.body = {
        code: 1,
        msg: "添加成功"
    }
})

// 删除数据
router.post('/list/del', async ctx => {
    let { delid } = ctx.request.body;

    let res = await global.query(`delete from list where id = ${delid}`)
    ctx.body = {
        code: 1,
        msg: "删除成功"
    }
})

// 获取某一条数据
router.post('/list/detail/:id', async ctx => {
    let { id } = ctx.params;

    let res = await global.query(`select * from list where id = ${id}`)

    ctx.body = {
        code: 1,
        msg: "获取成功",
        data: res
    }
})

// 修改数据
router.post('/list/edit', async ctx => {
    let { editid, title, type, status, author, ing } = ctx.request.body;

    console.log(ctx.request.body);

    let createTime = moment().format('YYYY MM DD HH:mm:ss')

    let res = await global.query(`update list set title='${title}',type='${type}',status='${status}',author='${author}',ing = '${ing}',createTime='${createTime}' where id = ${editid}`)
    ctx.body = {
        code: 1,
        msg: "修改成功",
    }
})

// 获取评论
router.post('/pl', async ctx => {
    let res = await global.query(`select * from pl`)
    ctx.body = {
        code: 1,
        msg: '获取成功',
        data: res
    }
})

// 添加评论
router.post('/pl/add', async ctx => {
    let { author, content } = ctx.request.body;

    let res = await global.query(`insert into pl(author, content) values('${author}','${content}')`);
    ctx.body = {
        code: 1,
        msg: '添加成功'
    }
})

// 删除评论
router.post('/pl/del', async ctx => {
    let { plid } = ctx.request.body;

    let res = await global.query(`delete from pl where id = '${plid}'`)
    ctx.body = {
        code: 1,
        msg: '删除成功'
    }
})

app.use(cors())
app.use(bodyParser());
app.use(router.routes());
app.use(router.allowedMethods());

app.listen(4000, () => {
    console.log("http://localhost:4000 后台服务器启动成功");
});